package longrise.phone.com.bjjt_henan.compensate;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.datasource.IncreasingQualityDataSourceSupplier;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.ui.AbstractSpinerAdapter;
import com.longrise.bjjt.util.UiUtil;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;

/**
 * 保险报案，Created by Administrator on 2016/5/12.
 */

public class InsuranceReportView extends LFView implements View.OnClickListener, AbstractSpinerAdapter.IOnItemSelectListener {
    private String singleCar;
    private Context context = null;
    private View view = null;
    private BaseApplication mApplication;
    private Button bt_promptaftercreateappoint_ensure;
    private Button bt_insurancereport;
    private Dialog processDialog;
    private RadioGroup rg_createinsurance1;
    private RadioButton rb_createinsurance1;
    private RadioButton rb_createinsurance2;
    private RadioButton rb_createinsurance3;
    private RadioButton otherMainRBtn;
    private RadioButton otherNextRBtn;
    private RadioGroup rg_createinsurance2;
    private RadioButton rb_createinsurance4;
    private RadioButton rb_createinsurance5;
    private RadioButton rb_createinsurance6;
    private RadioButton myMainRBtn;
    private RadioButton myNextRBtn;
    private EntityBean[] CreateAgreementbean;
    private String dutytype2;
    private String dutytype1;
    private TextView tv_insurancereport1;
    private TextView tv_insurancereport2;
    private TextView tv_insurancereport3;
    private TextView tv_insurancereport4;
    private TextView tv_insurancereport5;
    private TextView tv_insurancereport6;
    private String appCaseno;
    private String carownphone1;
    private String carownname1;
    private String casecarno1;
    private String carownphone2;
    private String carownname2;
    private String casecarno2;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private TextView tv_insurrancehe;
    private ImageView iv_insurancehe;
    private LinearLayout ll_insurancehe;

    private EntityBean[] cpBeans;
    private List<String> cpLists;//保险公司名称的集合
    private RelativeLayout otherComRLayout;
    private LinearLayout otherComLLayout;
    private EditText otherComET;
    private int otherSelect = -1;
    private RelativeLayout myComRLayout;
    private LinearLayout myComLLayout;
    private EditText myComET;
    private int mySelect = -1;

    public InsuranceReportView(Context c, EntityBean[] CreateAgreementbean, String appCaseno) {
        super(c);
        this.context = c;
        this.mApplication = (BaseApplication) c.getApplicationContext();
        this.CreateAgreementbean = CreateAgreementbean;
        this.appCaseno = appCaseno;
    }

    public InsuranceReportView(Context c, EntityBean[] CreateAgreementbean, String appCaseno, String singleCar) {
        super(c);
        this.context = c;
        this.mApplication = (BaseApplication) c.getApplicationContext();
        this.CreateAgreementbean = CreateAgreementbean;
        this.appCaseno = appCaseno;
        this.singleCar = singleCar;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    public void setCpBeans(EntityBean[] cpBeans) {
        this.cpBeans = cpBeans;
        if (null == cpLists) {
            cpLists = new ArrayList<String>();
        } else {
            cpLists.clear();
        }
        for (EntityBean bean : cpBeans) {
            cpLists.add(bean.getString("inscomname"));
        }
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_insurancereport, null);
        bt_insurancereport = (Button) view.findViewById(R.id.bt_insurancereport);
        rg_createinsurance1 = (RadioGroup) view.findViewById(R.id.rg_createinsurance1);
        rb_createinsurance1 = (RadioButton) view.findViewById(R.id.rb_createinsurance1);
        rb_createinsurance2 = (RadioButton) view.findViewById(R.id.rb_createinsurance2);
        rb_createinsurance3 = (RadioButton) view.findViewById(R.id.rb_createinsurance3);
        rg_createinsurance2 = (RadioGroup) view.findViewById(R.id.rg_createinsurance2);
        rb_createinsurance4 = (RadioButton) view.findViewById(R.id.rb_createinsurance4);
        rb_createinsurance5 = (RadioButton) view.findViewById(R.id.rb_createinsurance5);
        rb_createinsurance6 = (RadioButton) view.findViewById(R.id.rb_createinsurance6);
        otherMainRBtn = (RadioButton) view.findViewById(R.id.dispute_rb_other_main);
        otherNextRBtn = (RadioButton) view.findViewById(R.id.dispute_rb_other_next);
        myMainRBtn = (RadioButton) view.findViewById(R.id.dispute_rb_my_main);
        myNextRBtn = (RadioButton) view.findViewById(R.id.dispute_rb_my_next);
        tv_insurancereport1 = (TextView) view.findViewById(R.id.tv_insurancereport1);
        tv_insurancereport2 = (TextView) view.findViewById(R.id.tv_insurancereport2);
        tv_insurancereport3 = (TextView) view.findViewById(R.id.tv_insurancereport3);
        tv_insurancereport4 = (TextView) view.findViewById(R.id.tv_insurancereport4);
        tv_insurancereport5 = (TextView) view.findViewById(R.id.tv_insurancereport5);
        tv_insurancereport6 = (TextView) view.findViewById(R.id.tv_insurancereport6);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("保险报案");

        otherComRLayout = (RelativeLayout) view.findViewById(R.id.insurance_rl_otherins);
        otherComLLayout = (LinearLayout) view.findViewById(R.id.insurance_ll_otherins);
        otherComET = (EditText) view.findViewById(R.id.insurance_et_otherins);
        myComRLayout = (RelativeLayout) view.findViewById(R.id.insurance_rl_myins);
        myComLLayout = (LinearLayout) view.findViewById(R.id.insurance_ll_myins);
        myComET = (EditText) view.findViewById(R.id.insurance_et_myins);

        EntityBean otherBean = null;//对方信息
        EntityBean myBean = null;//我的信息

        if (TextUtils.equals("0", singleCar)) {
            myBean = CreateAgreementbean[0];
            tv_insurrancehe = (TextView) view.findViewById(R.id.tv_insurrancehe);
            tv_insurrancehe.setVisibility(View.GONE);
            iv_insurancehe = (ImageView) view.findViewById(R.id.iv_insurancehe);
            iv_insurancehe.setVisibility(View.GONE);
            ll_insurancehe = (LinearLayout) view.findViewById(R.id.ll_insurancehe);
            ll_insurancehe.setVisibility(View.GONE);
            TextView selfTitleTV = (TextView) view.findViewById(R.id.self_insuinfo_title);
            selfTitleTV.setVisibility(View.GONE);
            rb_createinsurance6.setVisibility(View.GONE);
            myMainRBtn.setVisibility(View.GONE);
            myNextRBtn.setVisibility(View.GONE);
            rb_createinsurance4.setChecked(true);//单车默认全责
        } else {//双车事故
            for (int i = 0; i < CreateAgreementbean.length; i++) {
                String ownother = CreateAgreementbean[i].getString("ownother");
                if (TextUtils.equals(ownother, "0")) {
                    myBean = CreateAgreementbean[i];
                } else if (TextUtils.equals(ownother, "1")) {
                    otherBean = CreateAgreementbean[i];
                }
            }
            //对方数据
            carownphone2 = (String) otherBean.get("carownphone");
            dutytype2 = (String) otherBean.get("dutytype");
            carownname2 = (String) otherBean.get("carownname");
            String id2 = (String) otherBean.get("id");
            casecarno2 = (String) otherBean.get("casecarno");

            if ("0".equals(dutytype2)) {
                rb_createinsurance1.setChecked(true);
                rb_createinsurance5.setChecked(true);
            }
            if ("1".equals(dutytype2)) {
                rb_createinsurance2.setChecked(true);
                rb_createinsurance4.setChecked(true);
            }
            if ("2".equals(dutytype2)) {
                rb_createinsurance3.setChecked(true);
                rb_createinsurance6.setChecked(true);
            }
            if ("3".equals(dutytype2)) {//对方主责
                otherMainRBtn.setChecked(true);
                myNextRBtn.setChecked(true);
            }
            if ("4".equals(dutytype2)) {//对方次责
                otherNextRBtn.setChecked(true);
                myMainRBtn.setChecked(true);
            }
            //对方的数据
            tv_insurancereport1.setText(carownname2);
            tv_insurancereport2.setText(casecarno2);
            tv_insurancereport3.setText(carownphone2);
        }
        //本方数据
        carownphone1 = (String) myBean.get("carownphone");
        dutytype1 = (String) myBean.get("dutytype");
        carownname1 = (String) myBean.get("carownname");
        String id1 = (String) myBean.get("id");
        casecarno1 = (String) myBean.get("casecarno");

        //本方的数据
        tv_insurancereport4.setText(carownname1);
        tv_insurancereport5.setText(casecarno1);
        tv_insurancereport6.setText(carownphone1);
    }

    private void regEvent(boolean b) {
        if (null != bt_insurancereport) {
            bt_insurancereport.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
        if (null != otherComLLayout) {
            otherComLLayout.setOnClickListener(b ? this : null);
        }
        if (null != myComLLayout) {
            myComLLayout.setOnClickListener(b ? this : null);
        }
    }

    //检测输入是否正确
    private boolean checkData() {
        if (TextUtils.equals("0", singleCar)) {
            if (mySelect == -1) {
                UiUtil.showToast(context, "请选择承保公司");
                return false;
            }
        } else {
            if (otherSelect == -1) {
                UiUtil.showToast(context, "请选择对方承保公司");
                return false;
            }
            if (mySelect == -1) {
                UiUtil.showToast(context, "请选择己方承保公司");
                return false;
            }
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_insurancereport:
                if (!checkData()) {
                    return;
                }
                if (TextUtils.equals("0", singleCar)) {
                    //单车提交
                    submitRespInfo("0");
                } else {
                    submitRespInfo("1");
                }
                break;
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;
            case R.id.insurance_ll_otherins://对方承保
                UiUtil.showSelectPopWindow(context, cpLists, this, otherComRLayout, true);
                break;
            case R.id.insurance_ll_myins://己方承保
                UiUtil.showSelectPopWindow(context, cpLists, this, myComRLayout, true);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(int pos, View view) {
        switch (view.getId()) {
            case R.id.insurance_rl_otherins:
                otherSelect = pos;
                otherComET.setText(cpLists.get(pos));
                break;
            case R.id.insurance_rl_myins:
                mySelect = pos;
                myComET.setText(cpLists.get(pos));
                break;
            default:
                break;
        }
    }

    //提交保险报案
    private void submitRespInfo(String mark) {
        processDialog = UiUtil.showProcessDialog(getContext(), "上传数据中");
        EntityBean bean = new EntityBean();
        bean.set("appcaseno", appCaseno);
        bean.set("casecarno", casecarno1);

        bean.set("casetelephone", carownphone1);
        // 经度
        bean.set("caselon", mApplication.getLongitude());
        // 纬度
        bean.set("caselat", mApplication.getLatitude());
        // 地址
        bean.set("caseaddress", mApplication.getAddress());
        // 提交时间
        bean.set("casedate", CommontUtils.getNowTime());
        bean.set("reportway", "0"); //保险报案方式
        // 区域
        bean.set("areaid", mApplication.getAreaid());
        bean.set("username", mApplication.getKckpName());
        bean.set("token", mApplication.getToken());
        EntityBean[] casecarlist = null;
        // 本方数据
        EntityBean bfBean = new EntityBean();
        bfBean.set("carownname", carownname1);
        bfBean.set("carownphone", carownphone1);
        bfBean.set("casecarno", casecarno1);
        EntityBean myCpBean = cpBeans[mySelect];
        bfBean.set("inscompany", myCpBean.getString("inscomname"));
        bfBean.set("inscomcode", myCpBean.getString("inscomcode"));
        bfBean.set("dutytype", dutytype1);
        if (!TextUtils.equals("0", singleCar)) {//双车
            // 对方数据
            EntityBean dfBean = new EntityBean();
            dfBean.set("carownname", carownname2);
            dfBean.set("carownphone", carownphone2);
            dfBean.set("casecarno", casecarno2);
            EntityBean otherCpBean = cpBeans[otherSelect];
            dfBean.set("inscompany", otherCpBean.getString("inscomname"));
            dfBean.set("inscomcode", otherCpBean.getString("inscomcode"));
            dfBean.set("dutytype", dutytype2);
            if (TextUtils.equals(dutytype2, "0")) {//对方全责
                bean.set("inscomcode", otherCpBean.getString("inscomcode"));
            } else if (TextUtils.equals(dutytype1, "0")) {
                bean.set("inscomcode", myCpBean.getString("inscomcode"));
            }
            casecarlist = new EntityBean[]{bfBean, dfBean};
        } else {
            bfBean.set("dutytype", "0");
            bean.set("inscomcode", myCpBean.getString("inscomcode"));
            casecarlist = new EntityBean[]{bfBean};
        }
        bean.set("casecarlist", casecarlist);

        LoadDataManagerCar.getInstance(context).callService(null,
                mApplication.getServerUrl(), URLConstant.SUBMITCASEINFORINSCOMPANY, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        Log.i("submitCaseInforInscomp", result.toString());
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        String redes = bean.getString("redes");
                        if ("1".equals(restate)) {
                            NewLossLoadPicView lView = new NewLossLoadPicView(context, appCaseno, 0);
                            lView.setCaseType(singleCar);
                            FrameworkManager.getInstance().showNewForm(context, lView);
                        } else {
                            UiUtil.showToast(context, redes);
                        }

                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        if (resultType == LoadDataManagerFather.ResultType.Failure) {
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        } else if (resultType == LoadDataManagerFather.ResultType.NoNetWorks) {
                            UiUtil.showToast(context, "无网络连接，请检查您的网络");
                        }
                        return;
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                    }
                });

    }

}
